home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / win / pascal / center.pas < prev    next >
Pascal/Delphi Source File  |  1991-11-13  |  4KB  |  104 lines

  1. Unit Center;
  2. {**************************************************************************}
  3. {*    Center   by Daniel Thomas  CIS 72301,2164                           *}
  4. {*                                                                        *}
  5. {*  This code is hereby donated to the public domain.  Enjoy.             *}
  6. {*                                                                        *}
  7. {*  This unit contains a procedure, CenterPopup, which will center a      *}
  8. {*  Popup window (i.e. a dialog) in it's parent's window.  If it won't    *}
  9. {*  fit inside the parent's window, then it will be centered on top of    *}
  10. {*  the parent.                                                           *}
  11. {*                                                                        *}
  12. {*  Also, if the dialog would be positioned off the screen, it is forced  *}
  13. {*  within the visible screen.                                            *}
  14. {*                                                                        *}
  15. {*  There are a few descendant objects - tCenteredDialog and              *}
  16. {*  tCenteredInputDialog - that make using it a snap.  Just replace an    *}
  17. {*  occurrance of pDialog with pCenteredDialog, and you've got a centered *}
  18. {*  dialog!                                                               *}
  19. {**************************************************************************}
  20.  
  21. Interface
  22.  
  23. USES WinTypes,WinProcs,WObjects,StdDlgs;
  24.  
  25. Type
  26.   pInteger=^integer;
  27.  
  28.   pCenteredDialog=^tCenteredDialog;
  29.   tCenteredDialog=object(tDialog)
  30.       Procedure SetupWindow; virtual;
  31.     end;
  32.  
  33.   pCenteredInputDialog=^tCenteredInputDialog;
  34.   tCenteredInputDialog=object(tInputDialog)
  35.       Procedure SetupWindow; virtual;
  36.     end;
  37.  
  38. Procedure CenterPopup(aPopup,aParent: hWnd);
  39.  
  40. Implementation
  41.  
  42. Procedure CenterPopup(aPopup,aParent: hWnd);
  43.  
  44. var
  45.   PopupR,ParentR  : tRect;
  46.   ScreenW,ScreenH : integer;
  47.   x,y,
  48.   PopupW,PopupH,
  49.   ParentW,ParentH : word;
  50.  
  51.   procedure SetupValues(Wnd: hWnd; var R: tRect; var W,H : word);
  52.     begin
  53.       GetWindowRect(Wnd,R);
  54.       W := R.Right-R.Left;
  55.       H := R.Bottom-R.Top;
  56.     end; {SetupValues}
  57.  
  58.   procedure SetupLocation(PopupSize,ScreenSize,ParentSize,ParentStart : word;
  59.                           var PopupStart: word);
  60.     begin
  61.       if PopupSize > ScreenSize then
  62.         PopupStart := 0
  63.       else
  64.         begin
  65.           if PopupSize <= ParentSize then
  66.             PopupStart := ParentStart+((ParentSize-PopupSize) div 2)
  67.           else
  68.             PopupStart := ParentStart-((PopupSize-ParentSize) div 2);
  69.           if PopupStart > ScreenSize then
  70.             PopupStart := 0
  71.           else
  72.           if PopupStart+PopupSize > ScreenSize then
  73.             PopupStart := ScreenSize-PopupSize;
  74.         end;
  75.     end; {SetupLocation}
  76.  
  77. begin {CenterPopup}
  78.   ScreenW := GetSystemMetrics(sm_CXScreen);
  79.   ScreenH := GetSystemMetrics(sm_CYScreen);
  80.   SetupValues(aPopup,PopupR,PopupW,PopupH);
  81.   SetupValues(aParent,ParentR,ParentW,ParentH);
  82.   SetupLocation(PopupW,ScreenW,ParentW,ParentR.Left,x);
  83.   SetupLocation(PopupH,ScreenH,ParentH,ParentR.Top,y);
  84.   MoveWindow(aPopup,x,y,PopupW,PopupH,false);
  85. end; {CenterPopup}
  86.  
  87. Procedure tCenteredDialog.SetupWindow;
  88.  
  89. begin
  90.   tDialog.SetupWindow;
  91.   CenterPopup(HWindow, Parent^.HWindow);
  92. end; {tAniOptionsDialog.SetupWindow}
  93.  
  94. Procedure tCenteredInputDialog.SetupWindow;
  95.  
  96. begin
  97.   tInputDialog.SetupWindow;
  98.   CenterPopup(HWindow, Parent^.HWindow);
  99. end; {tAniOptionsDialog.SetupWindow}
  100.  
  101.  
  102.  
  103. end.
  104.